#include "file_system.h"


FileSystem::FileSystem()
{
    init_gui();
    timecal = millis()-5000;
    if (WiFi.getMode()==WIFI_STA&&WiFi.status()==WL_CONNECTED)
    {
        ssid = WiFi.SSID();
        ip = WiFi.localIP().toString();
        is_connected = true;
    }
    else if(WiFi.getMode()==WIFI_AP)
    {
        ssid = WiFi.softAPSSID();
        ip = WiFi.softAPIP().toString();
        is_connected = true;
    }
    else
    {
        ssid = "T_T";
        ip = "0.0.0.0";
        is_connected = false;
    }
    show_msg(LV_SYMBOL_FILE, ssid, ip);
    save_cfg.begin("Config");
    if (is_connected)
    {
        server = new WiFiServer(90);
        server->begin();
        num_sta = WiFi.softAPgetStationNum();
    }
}

void FileSystem::init_gui()
{
    label_scr = lv_obj_create(NULL, NULL);
    lv_obj_set_style_local_bg_color(label_scr, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_scr_load(label_scr);

    lv_style_init(&font_style);
	lv_style_set_text_font(&font_style, LV_STATE_DEFAULT, &lv_font_montserrat_34);
	lv_style_set_text_color(&font_style, LV_STATE_DEFAULT, LV_COLOR_BLUE);

    label_msg = lv_label_create(label_scr, NULL);
    label_sym = lv_label_create(label_scr, NULL);
    label_ssid = lv_label_create(label_scr, NULL);
	lv_obj_add_style(label_msg, LV_LABEL_PART_MAIN, &font_style);
    lv_obj_add_style(label_sym, LV_LABEL_PART_MAIN, &font_style);
    lv_obj_add_style(label_ssid, LV_LABEL_PART_MAIN, &font_style);
}

Event FileSystem::main_process(Action act)
{
    event = NOEVENT;
    if (is_connected)
    {
        tem_num_sta = WiFi.softAPgetStationNum();
        if(num_sta != tem_num_sta)
        {
            num_sta = tem_num_sta;
            show_msg(LV_SYMBOL_FILE, ssid, ip);
            event = RED;
        }
        if (!is_con){
            client = server->available();
            if (client)
            {
                is_con=true;
                is_input = true;
                event = RED;
            }
        }
        else{
            if (client.connected()){
                get_command();
            }
            else
            {
                is_con = false;
            }
        }
    }
    return event;
}

void FileSystem::show_msg(String sym, String ssid, String ip)
{
    lv_label_set_text(label_msg, ip.c_str());
    lv_label_set_text(label_sym, sym.c_str());
    lv_label_set_text(label_ssid, ssid.c_str());
    lv_obj_align(label_msg, NULL, LV_ALIGN_CENTER, 0, 50);
    lv_obj_align(label_sym, NULL, LV_ALIGN_CENTER, 0, -50);
    lv_obj_align(label_ssid, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_task_handler();
}

FileSystem::~FileSystem()
{

    lv_obj_clean(label_scr);
    if (is_connected)
    {
        server->close();
        delete server;
    }
    
}

void FileSystem::get_command(){
  if (is_input)
  {
    client.write(("#Poki(" + cur_path + "/):").c_str());
    client.write_P("\0", 1);
    is_input = false;
  }
  if(client.available()){
    is_input = true;
    char sin_byte;
    String comdata = "";
    while(true)
    {
        sin_byte = client.read();
        if(sin_byte=='\0')
          break;
        comdata += sin_byte;
    }
    if (comdata.startsWith("send_file"))
    {
        recv_file(comdata);
    }
    else if (comdata.startsWith("ssid")||comdata.startsWith("password")||comdata.startsWith("cityname")||comdata.startsWith("key")||comdata.startsWith("view"))
    {
        command_setting(comdata);
        event = GREEN;
    }
    else{
      command_base(comdata);
      event = GREEN;
    }
    
  }
}

void FileSystem::command_setting(String cmd)
{
    // return;
    int blank_pos;
    blank_pos = cmd.indexOf(' ');//检测字符串中的逗号
    String cmd_dict, cmd_var;
    bool cmd_error=false;
    if(blank_pos != -1)
    {
        cmd_dict = cmd.substring(0, blank_pos);
        cmd_var = cmd.substring(blank_pos+1, cmd.length());
        if (cmd_dict == "ssid")
        {
            save_cfg.putString("ssid", cmd_var);
            client.write("ssid setting: ");
            client.write((save_cfg.getString("ssid", "") + "\n").c_str());
        }

        else if (cmd_dict == "password")
        {
            save_cfg.putString("password", cmd_var);
            client.write("password setting: ");
            client.write((save_cfg.getString("password", "") + "\n").c_str());
        }
        else if (cmd_dict == "cityname")
        {
            save_cfg.putString("cityname", cmd_var);
            client.write("cityname setting: ");
            client.write((save_cfg.getString("cityname", "") + "\n").c_str());
        }
        else if (cmd_dict == "key")
        {
            save_cfg.putString("key", cmd_var);
            client.write("key setting: ");
            client.write((save_cfg.getString("key", "") + "\n").c_str());
        }
        else if (cmd_dict == "view")
        {
            if (cmd_var=="ssid")
            {
                client.write("ssid: ");
                client.write((save_cfg.getString("ssid", "") + "\n").c_str());
            }
            else if(cmd_var=="password")
            {
                client.write("password: ");
                client.write((save_cfg.getString("password", "") + "\n").c_str());
            }
            else if(cmd_var=="cityname")
            {
                client.write("cityname: ");
                client.write((save_cfg.getString("cityname", "") + "\n").c_str());
            }
            else if(cmd_var=="key")
            {
                client.write("key: ");
                client.write((save_cfg.getString("key", "") + "\n").c_str());
            }
            else{
                cmd_error=true; 
            }
        }
        else
          cmd_error=true;    
    }
    else
    {
     cmd_error=true;
    }
    if (cmd_error)
    {
       client.write("command not found!\n");
    }
}